import { getLogin,checkToken,getMobile } from "../../api/index.js"
Page({
  data() {
    return {
      userInfo: {},
      loading: false,
      isLogin: true,
      mobile:"-"
    };
  },
  methods: {
    navigateToMore() {
      console.log('路由跳转')
      // this.$router.push('/pages/more/index')
      this.$router.replace('/pages/download/index')
    },
    getSystemInfo() {
      ok.showToast('系统信息已打印到控制台')
      // ok.getSystemInfo()
      //   .then(res => {
      //     ok.showToast('系统信息已打印到控制台')
      //     console.log('getSystemInfo res', res.data)
      //   })
    },
    login() {


      ok.login()
        .then(res => {

          // console.log('小程序临时登录凭证22 h5appCode', res.data.h5appCode);
          let h5appCode = res.data.h5appCode
          this.loading = true
          getLogin({
            h5appCode
          }).then(res => {
            let { data, code } = res.data
            if (code === 0) {
              this.loading = false
              this.$router.push('/pages/entrance/index')
              ok.setH5AppStorage({
                key: 'token',
                data: data.token
              }).catch(err => {
                console.log("接口调用异常", err);
              });
            } else {
              this.loading = false
              ok.showToast('登录失败')
            }

          })


        })
        .catch(err => {
          this.loading = false
          ok.showToast('登录失败')
        });
    }
  },
  mounted() {

    console.log('点击了');
    ok.login()
        .then(res => {
          console.log(res.data);
          console.log('小程序临时登录凭证234 h5appCode', res.data.h5appCode);
          let h5appCode = res.data.h5appCode
          // return
          getMobile({
            h5appCode
          }).then(res => {
            let { data, code } = res.data
            console.log(data,code)
            this.mobile = data.mobile
          })


          this.loading = true
        ok.getH5AppStorage({
          key: 'token'
        }).then(async res => {
          
          if(!res.data.data)  {
            this.loading = false
            return
          }
          const { data } = await checkToken({})
          if (data.code === 500 && data.msg === '用户登录已超时，请重新登录') {
            ok.clearH5AppStorage().then(res=>{
              this.loading = false
            })
            .catch(err=> {
              console.log("接口调用异常",err);
            });
            
            return
          }
          // 返回data为true就是过期了
          if(data.data){
            this.loading = false
            return
          }

          this.loading = false
          this.$router.push('/pages/entrance/index')


        }).catch(err => {
          this.loading = false
          console.log("接口调用异常", err);
        });


        })
        .catch(err => {
          ok.showToast('登录失败')
        });
    



    
  }
});
